window.onload = function(){
    layui.use(['jquery','form','layer'],function(){
        var $ = layui.jquery, form = layui.form;

        form.on('checkbox(check)',function(res){
            var _this=res.elem;
            var name = _this.className;
            var is_check=res.elem.checked
            switch(name){
                case "listTh":
                var prevAll=$(this).parents(".contbox3").prevAll();
                var len=prevAll.length;
                for(var i=0;i<len;i++){
                    if(prevAll[i].classList[1]=="contbox2"){
                        prevAll[i].children[0].checked=true;
                        break;
                    }
                }
                for(var i=0;i<len;i++){
                    if(prevAll[i].classList[1]=="contbox1"){
                        prevAll[i].children[0].checked=true;
                        break;
                    }
                };
                form.render()
                break;
                
                case "listT":
                if(is_check){
                    var prevAll=$(this).parents(".contbox2").prevAll();
                    var nextAll=$(this).parents(".contbox2").nextAll();                
                    var len=prevAll.length;
                    var len1=nextAll.length;
                    for(var i=0;i<len;i++){
                        if(prevAll[i].classList[1]=="contbox1"){
                            prevAll[i].children[0].checked=true;
                            break;
                        }
                    };
                    for(var i=0;i<len1;i++){
                        if(nextAll[i].classList[1]=="contbox3"){
                            nextAll[i].children[0].checked=true;
                        }else{
                            break;
                        }
                    };
                }else{
                    var nextAll=$(this).parents(".contbox2").nextAll();
                    var len=nextAll.length;
                    for(var i=0;i<len;i++){
                        if(nextAll[i].classList[1]=="contbox3"){
                            nextAll[i].children[0].checked=false;
                        }else{
                            break;
                        }
                    }
                };
                form.render()
                break;
    
                case "listO":
                if(is_check){
                    var nextAll=$(this).parents(".contbox1").nextAll();
                    var len=nextAll.length;
                    for(var i=0;i<len;i++){
                        if(nextAll[i].classList[1]!="contbox1"){
                            nextAll[i].children[0].checked=true;
                        }else{
                            break;
                        }
                    }
                }else{
                    var nextAll=$(this).parents(".contbox1").nextAll();
                    var len=nextAll.length;
                    for(var i=0;i<len;i++){
                        if(nextAll[i].classList[1]!="contbox1"){
                            nextAll[i].children[0].checked=false;
                        }else{
                            break;
                        }
                    }
                };
                form.render()
                break;
            } 
        })

        $(".btn-save").on("click",function(){
            var btngroup=$("input[type=checkbox]:checked"),
                arr=[];len=btngroup.length;
            var group_name=$(".group_name").val();
            var id=$(".hide-id").val();
    
            for(var i=0;i<len;i++){
                arr.push(btngroup[i].value);
            }
            if(group_name!=""){
                $.ajax({
                    url:"/permission/update-group",
                    type:"post",
                    data:{group_name:group_name,group_jurisd_id:arr,group_id:id},
                    dataType:"json",
                    success:function(data){
                        if(data.code==200){
                            Utils.layer.msg(data.msg);
                            setTimeout(function(){
                                location.href="/permission/index";
                            },1200)
                        }else{
                            Utils.layer.msg(data.msg)
                        }
                    }
                })
            }else{
                Utils.layer.msg("请填写权限名称")
            }
        })

    })
}